<?php

class Agenda_Model extends Model{
	
	private $id_contact;
	private $name;
	private $id_user;
	private $cel;
	private $tel;
	private $email;
	private $twitter;
	
	private $sqlInsert = "INSERT INTO contacts (name,id_user,cel,tel,email,twitter) 
						  VALUES ('%s','%s','%s','%s','%s','%s')";
						  
	private $sqlUpdate = "UPDATE contacts SET name='%s',cel='%s',tel='%s',email='%s',twitter='%s'
						  WHERE id_contact='%s'";
	
	private $sqlDelete = "DELETE FROM contacts WHERE id_contact='%s' AND id_user='%s'";
	
	private $sqlSelect = "SELECT * FROM contacts WHERE 1=1 %s %s";
	
	public function setIdContact($id_contant){
		$this->id_contact = trim(strip_tags($id_contant));	
	}
	
	public function setName($name){
		$this->name = trim(strip_tags($name));	
	}
	
	public function setIdUser($id_user){
		$this->id_user = trim(strip_tags($id_user));	
	}
	
	public function setCel($cel){
		$this->cel = trim(strip_tags($cel));	
	}
	
	public function setTel($tel){
		$this->tel = trim(strip_tags($tel));	
	}
	
	public function setEmail($email){
		$this->email = trim(strip_tags($email));	
	}
	
	public function setTwitter($twitter){
		$this->twitter = trim(strip_tags($twitter));	
	}
	
	public function getIdContact(){
		return $this->id_contact;	
	}
	
	public function getName(){
		return $this->name;	
	}
	
	public function getIdUser(){
		return $this->id_user;	
	}
	
	public function getCel(){
		return $this->cel;	
	}
	
	public function getTel(){
		return $this->tel;	
	}
	
	public function getEmail(){
		return $this->email;	
	}
	
	public function getTwitter(){
		return $this->twitter;	
	}
	
	public function insert(){
		$sql = sprintf($this->sqlInsert,$this->getName(),$this->getIdUser(),$this->getCel(),$this->getTel(),
					   $this->getEmail(),$this->getTwitter());
		return $this->runQuery($sql);	
	}
	
	public function update(){
		$sql = sprintf($this->sqlUpdate,$this->getName(),$this->getCel(),$this->getTel(),
					   $this->getEmail(),$this->getTwitter(),$this->getIdContact());
		return $this->runQuery($sql);	
	}
	
	public function delete(){
		$sql = sprintf($this->sqlDelete,$this->getIdContact(),$this->getIdUser());
		return $this->runQuery($sql);	
	}
	
	public function select($where='',$order=''){
		$sql = sprintf($this->sqlSelect,$where,$order);
		return $this->runSelect($sql);	
	}
	
	public function load(){
		$where = sprintf("AND id_contact='%s'",$this->getIdContact());
		$row = $this->select($where);
		$this->setIdContact($row[0]['id_contact']);
		$this->setName($row[0]['name']);
		$this->setIdUser($row[0]['id_user']);
		$this->setCel($row[0]['cel']);
		$this->setTel($row[0]['tel']);
		$this->setEmail($row[0]['email']);
		$this->setTwitter($row[0]['twitter']);
		return $this;
	}
	
	public function loadLista(){
		$where = sprintf("AND id_user='%s'",$_SESSION['id_user']);
		return $row = $this->select($where);
	}
	
}

?>